<?php
/**
 * Created by PhpStorm.
 * User: wangsentao
 * Date: 2018/1/30
 * Time: 下午2:37
 */

namespace app\common\Utils;

use app\common\ShaoHuoPay\Pay;
use app\api\controller\Errorcode;

class WxPay
{
    protected static $wechat;
    protected $config;

    public function __construct($type)
    {
        if ($type == '3' || $type == '4') {
            $tm = [
                'app_id' => config('pay')['WX_MP_PAY']['APPID'], // 公众号 APPID
                'miniapp_id' => config('pay')['WX_WECHAT_PAY']['APPID'], // 小程序 APPID
                'mch_id' => config('pay')['wx_mchid_mini'],
                'key' => config('pay')['wx_key_mini'],
                'notify_url' => config('pay')['wx_notiful_mini'],
            ];
        } else {
            $tm = [ 
                'appid' => config('pay')['WX_PAY']['APPID'], // APP APPID
                'mch_id' => config('pay')['WX_MCHID'],
                'key' => config('pay')['WX_KEY'],
                'notify_url' => config('pay')['WX_NOTIFUL'],
            ];
        }
        $common_config = [
            'cert_client' => dirname(dirname(__DIR__)) . '/common/Wxcert/apiclient_cert.pem', // optional，退款等情况时用到
            'cert_key' => dirname(dirname(__DIR__)) . '/common/Wxcert/apiclient_key.pem',// optional，退款等情况时用到
        ];
        $this->config = array_merge($tm, $common_config);

        self::$wechat = Pay::wechat($this->config);
    }

    /**
     * app 支付
     * 客户端传过来的金额为元 调用微信接口的时候要转化为分
     * @param $order_info
     */
    public function appPay($order_info)
    {
        if (PayMent::PAY_WECHAT_APP != $order_info['shareId']) {
            return [
                'msg' => Errorcode::$code['400001'] . $order_info['shareId'],
            ];
        }

        $order = [
            'out_trade_no' => trim($order_info['order_num']),//生成系统订单号
            'total_fee' => config('app_debug') == true ? 1 : intval(trim($order_info['totalFee'])),
            'body' => trim($order_info['body']),
        ];
        $res = self::$wechat->app($order)->getContent();

        return json_decode($res, true);
    }

    /**
     * 公众号
     */
    public function mpPay($order_info)
    {
        if (PayMent::PAY_MP_JS != $order_info['shareId']) {
            return [
                'msg' => Errorcode::$code['400001'] . $order_info['shareId'],
            ];
        }

        $order = [
            'out_trade_no' => $order_info['order_num'],//生成系统订单号
            'body' => $order_info['body'],
            'total_fee' => intval($order_info['totalFee']),
            'openid' => $order_info['openid'],
        ];

        return self::$wechat->mp($order)->all();
    }

    /**
     * 小程序
     */
    public function miniPay($order_info)
    {
        if (PayMent::PAY_MINI_JS != $order_info['shareId']) {
            return [
                'msg' => Errorcode::$code['400001'] . $order_info['shareId'],
            ];
        }

        $order = [
            'out_trade_no' => $order_info['order_num'],//生成系统订单号
            'body' => $order_info['body'],
            'total_fee' => config('app_debug') == true ? 1 :intval($order_info['totalFee']),
            'openid' => $order_info['openid'],
        ];

        return self::$wechat->miniapp($order)->all();

    }

    /**
     * 订单退款
     * @param $refound_order
     * @return mixed
     */
    public function refound($refound_order)
    {
        $order = [
            'out_trade_no' => $refound_order['out_trade_no'],
            'out_refund_no' => time(),
            'total_fee' => $refound_order['total_amount'],
            'refund_fee' => $refound_order['total_amount'],
            'refund_desc' => '久多订单退款',
        ];

        try {
            $res = self::$wechat->refund($order);

            if($res->code == '10000' && $res->msg == 'Success'){
                return ['code'=>'success','msg'=>'退款成功'];
            }
        } catch (Exception $exception){
            return ['code'=>'fail','msg'=> $exception->getMessage()];
        }
    }
}